package com.iwayin.core.bo.fans;

import java.util.ArrayList;
import java.util.List;

import com.iwayin.core.bo.BaseBO;
import com.iwayin.core.dao.fans.FansDAO;
import com.iwayin.core.dao.follow.FollowDAO;
import com.iwayin.core.dao.user.UserDAO;
import com.iwayin.core.dataobject.FansDO;
import com.iwayin.core.dataobject.FollowDO;
import com.iwayin.core.dataobject.UserDO;

public class FansBOImpl extends BaseBO implements FansBO {

	private FansDAO fansDAO;
	private UserDAO userDAO;
	private FollowDAO followDAO;

	@Override
	public List<UserDO> findFansList(Long loginUserId, FansDO query) {
		List<FansDO> fansList = fansDAO.findFansList(query);
		List<UserDO> userList = new ArrayList<UserDO>(fansList.size());

		// 补全粉丝信息
		for (FansDO fan : fansList) {
			UserDO user = userDAO.getUserById(fan.getFansId());
			userList.add(user);
			if (loginUserId == null) {
				continue;
			}
			if(user.getId().equals(loginUserId)){
				user.setMySelf(true);
				continue;
			}
			FollowDO isFollow = followDAO.getFollower(loginUserId, fan.getFansId());
			// 判断与当前登录用户关系
			user.setFollow(isFollow == null ? false : true);
		}
		return userList;
	}

	@Override
	public List<UserDO> findFansMostToday(Long size) {
		List<FansDO> fans = this.fansDAO.FindFansMostToday(size);
		List<UserDO> users = new ArrayList<UserDO>();
		if (fans != null&&fans.size()>0) {
			for (FansDO f : fans) {
				UserDO user = this.userDAO.getUserById(f.getUserId());
				user.setFansCountToday(f.getFansCountToday());
				users.add(user);
			}
		}
		return users;
	}

	// spring 注入
	public void setFansDAO(FansDAO fansDAO) {
		this.fansDAO = fansDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public void setFollowDAO(FollowDAO followDAO) {
		this.followDAO = followDAO;
	}

}
